/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ficherofiguras;

/**
 *
 * @author AULAC
 */

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class FicheroFiguras {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ClassNotFoundException {
        
        Scanner sc = new Scanner(System.in);
        
        ArrayList<Figura> figuras = new ArrayList<Figura>();
        int contTri=0, contRec=0, contCir=0;
        
        //Hola
        
        //Creación de las figuras
        
        Triangulo t1 = new Triangulo((float)3.0, (float)4.5, "Rojo");
        Triangulo t2 = new Triangulo((float)3.2, (float)4.0, "Verde");
        Rectangulo r1 = new Rectangulo((float)2.2, (float)5.0, "Amarillo");
        Rectangulo r2 = new Rectangulo((float)4.3, (float)2.0, "Negro");
        Circulo c1 = new Circulo((float)2.2, "Azul");
        
        //Añadir figuras a la colección
        
        figuras.add(t1);
        figuras.add(t2);
        figuras.add(r1);
        figuras.add(r2);
        figuras.add(c1);
        
        //Creación de ficheros y volcado
        
        File f = new File("Figuras.data");
        
        ObjectOutputStream out = null;
        ObjectInputStream in = null;
        
        try{
            
            out = new ObjectOutputStream(new FileOutputStream(f));            
            out.writeObject(figuras);           
            out.close();
            
            in = new ObjectInputStream(new FileInputStream(f));
            
            for(Figura fig: figuras){                
                if(fig instanceof Triangulo){
                    Triangulo tr1 = (Triangulo)fig;
                    System.out.println(tr1.toString() + " | Area: "+tr1.calcularArea());
                    contTri++;
                }else if(fig instanceof Rectangulo){
                    Rectangulo re1 = (Rectangulo)fig;
                    System.out.println(re1.toString() + " | Area: "+re1.calcularArea());
                    contRec++;
                }else{
                    Circulo ci1 = (Circulo)fig;
                    System.out.println(ci1.toString() + " | Area: "+ci1.calcularArea());
                    contCir++;
                }
            }
            
            System.out.println("");
            System.out.println("Número de figuras: ");
            System.out.println("Triángulos: "+contTri);
            System.out.println("Rectángulos: "+contRec);
            System.out.println("Círculos: "+contCir);
            
            in.close();
            
            
            
        }catch(FileNotFoundException ex){
            
            System.out.println("Fichero no encontrado");
            
        }catch(IOException ex){
            
            System.out.println("Error de escritura: "+ex.getMessage());
            
        }
        
        
        
    }

}
